<template>
  <div>
    <!-- 个人充值 -->
    <a-spin :spinning="spinning">
      <a-form class="a_form_sty" @submit="handleInput" :form="orgform">
        <div class="selectcard">
          <a-row style="margin-bottom: 30px;">
            <a-col :sm="{ span:8 }">
              <div style="text-align: right;font-size: 20px;font-weight: 600;">快捷支付</div>
            </a-col>
            <a-col :xs="{ span : 24 }" :sm="{ span:10,offset:1 }"></a-col>
          </a-row>
          <a-row>
            <!-- <div class="card_sty radio_card"> -->
            <a-form-item
              required
              style
              label="选择银行卡"
              :label-col="{ span: 8}"
              :wrapper-col="{ span: 14,offset:1 }"
            >
              <a-radio-group @change="onChange" style="width:100%;" v-model="radiovalue">
                <ul v-if="!showAllBankList">
                  <li
                    @click="getRadioValue(one.accountNo)"
                    class="li_radio_style"
                    :index="index"
                    v-for="(one,index) in secondBankList"
                    :key="one.cardNo"
                  >
                    <div class="bank_img">
                      <a-radio :style="radioStyle" :value="one.accountNo"></a-radio>
                      <img src="/static/img/default_avatar.png" alt />
                    </div>
                    <span class="bank_info">{{one.bankName}}</span>
                    <span class="bank_info">{{one.cardNo | cardNofilter}}</span>
                    <!-- <i>2小时</i> -->
                  </li>
                </ul>
                <ul v-if="showAllBankList">
                  <li
                    class="li_radio_style"
                    :index="index"
                    v-for="(one,index) in sites"
                    :key="one.cardNo"
                    @click="getRadioValue(one.accountNo)"
                  >
                    <div class="bank_img">
                      <a-radio :style="radioStyle" :value="one.accountNo"></a-radio>
                      <img src="/static/img/default_avatar.png" alt />
                    </div>
                    <span class="bank_info">{{one.bankName}}</span>
                    <span class="bank_info">{{one.cardNo | cardNofilter}}</span>
                    <!-- <i>2小时</i> -->
                  </li>
                </ul>
                <div class="addBank">
                  <div v-if="showAddbank" id="addBank" @click="goAddBank">
                    <a
                      href="javascript:false;void(0);"
                      style="text-decoration: none;color:#279e71;"
                      @click="goAddBank"
                    >+&ensp;添加银行卡</a>
                  </div>
                  <span v-if="isshowAll" @click="showAllbank" style="font-size: 13px;">{{showAll}}</span>
                </div>
              </a-radio-group>
            </a-form-item>
            <!-- </div> -->
          </a-row>
        </div>

        <div class="amount">
          <a-form-item
            style
            label="充值金额"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15,offset:1 }"
          >
            <div class="cash_out">
              <a-input
                v-decorator="[
                                'amountt',
                                { rules: [{
                              required: true,
                              message: '请输入数字' },{validator: validateCode}]
                              }
                          ]"
                size="large"
                placeholder="请输入金额"
              ></a-input>&nbsp;元
              <div class="tooltip_sty">
                <div id="tooltip_sty">单笔小于{{singleAmountLimit}}万，单日小于{{dayAmountLimit}}万</div>
              </div>
              <!-- ,
              validateTrigger: 'blur'-->
            </div>

            <div
              style="color:#818181;font-size:12px;text-align: left;"
            >充值手续费：{{ServiceCharge}}%，将从充值金额中直接扣除</div>
          </a-form-item>
          <a-form-item
            style
            label="充值说明"
            :label-col="{ span: 8}"
            :wrapper-col="{ span: 15,offset:1 }"
          >
            <div class="cash_out">
              <a-input v-model="perchargenote" size="large" placeholder="请填写充值说明"></a-input>
              <!-- <span class="tooltip_sty">单笔小于5万 单日小于5万</span> -->
            </div>
          </a-form-item>
          <!-- <div style=" width: 188px;padding-top: 10px">
                  <router-link to="/perindex">返回</router-link>
          </div>-->
          <a-row>
            <a-col :sm="{ span:8 }"></a-col>
            <a-col :xs="{ span : 24 }" :sm="{ span:10,offset:1 }">
              <a-button type="primary" html-type="submit">下一步</a-button>
            </a-col>
          </a-row>
        </div>
      </a-form>
    </a-spin>
  </div>
</template>

<script>
import { JSEncrypt } from "jsencrypt";
import AHideData from "../common/HideData";
import api from "@/common/api";
export default {
  components: {
    AHideData
  },
  data() {
    return {
      cash: "",
      sitesNo: "",
      demo: "",
      amountSum: "",
      bankCardNO: "",
      orgform: this.$form.createForm(this),
      secondBankList: [],
      spinning: true,
      sites: [],
      showAddbank: true,
      isshowAll: true,
      showAll: "展开",
      showAllBankList: false,
      radioStyle: {
        display: "block",
        height: "40px",
        lineHeight: "40px"
      },
      visible: true,
      defaultVisible: true,
      radiovalue: "",
      isbtn: false,
      customerOrg: {},
      customer: {
        status: ""
      },
      dayRechargeAmountSum: "",
      singleAmountLimit: "",
      dayAmountLimit: "",
      ServiceCharge: "",
      perchargenote: ""
    };
  },
  filters: {
    cardNofilter(value) {
      if (value) {
        var reg = /.*(.{4})$/;
        return value.replace(reg, "**** **** **** $1");
      }
    },
    cashfilter(value) {
      if (value) {
        return Number(value).toFixed(2);
      }
    }
  },
  watch: {},
  created() {
    //个人客户
    if (this.$store.state.memberType == "CU") {
      this.selectCUData();
    } //单位客户
    else if (this.$store.state.memberType == "CBM") {
      this.selectCBData();
    } //特约商户
    else {
      alert("未识别的账户");
      this.$router.push({ path: "/userMain" });
    }
  },
  mounted() {
    console.log("this.sites[0].accountNo", this.sitesNo);
    // this.radiovalue = this.sites[0].accountNo;
    // console.log('this.radiovalue',this.radiovalue)
  },

  methods: {
    getRadioValue(value) {
      console.log(value);
      this.radiovalue = value;
      this.bankCardNO = value;
    },
    goAddBank() {
      // this.$router.push("/per/bankadd/bankno");
      this.$router.push({
        path: "/per/bankadd/bankno",
        params: {
          banktypename: "储蓄卡"
        }
      });
      sessionStorage.setItem("addbankType", "3"); //1 为正常添加银行卡 2 为提现添加 3充值添加
    },
    allAashOut() {
      this.orgform.setFieldsValue({
        amountt: Number(this.cash) - 2
      });
    },
    handleInput(e) {
      e.preventDefault();
      if (this.bankCardNO !== "") {
      } else {
        this.$message.warning("请选择银行卡");
        return false;
      }
      this.orgform.validateFields((err, values) => {
        if (!err) {
          console.log("充值金额（分）", values.amountt * 100);
          console.log(this.bankCardNO);
          this.$axios
            .post("/agent/recharge/save", {
              bankAccountNo: this.bankCardNO,
              remark: this.perchargenote,
              amount: values.amountt * 100
            })
            .then(res => {
              console.log(res.data);
              if (res.data.code == "FAIL") {
                this.$message.error(res.data.message);
                // alert("提现金额小于可提现余额");
              } else if (res.data.code == "OK") {
                // console.log(values.amountt);
                console.log(res.data.data);
                this.$router.push({
                  name: "个人客户充值确认",
                  // path: "/cashmore-next",
                  params: {
                    seat: res.data.data
                  }
                });
              }
            });
        }
      });
    },
    validateCode(rule, value, callback) {
      if (
        value &&
        !/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(
          value
        )
      ) {
        callback("请输入正确的金额！");
        return;
      } else {
        callback();
      }
    },
    onChange(e) {
      console.log(e.target.value);
      this.bankCardNO = e.target.value;
    },
    selectCUData() {
      this.spinning = true;
      this.$axios.get("/agent/recharge/selRechargeCard").then(res => {
        if (res.data.code == "OK") {
          this.spinning = false;
          console.log("res:", res.data.data);
          console.log(
            "单日累计(元):",
            res.data.data.dayRechargeAmountSum / 100
          ); //单位是分 要除100
          console.log(
            "手续费:",
            JSON.parse(res.data.data.feeTemplate).rate * 100
          );
          console.log(
            "单笔限制:",
            res.data.data.product.singleAmountLimit / 100
          );
          console.log("单日限制:", res.data.data.product.dayAmountLimit / 100);
          console.log("交易限制:", res.data.data.product.open);

          if ((res.data.data.product.open = true)) {
            this.dayRechargeAmountSum =
              res.data.data.dayRechargeAmountSum / 100;
            this.singleAmountLimit =
              res.data.data.product.singleAmountLimit / 1000000;
            this.dayAmountLimit =
              res.data.data.product.dayAmountLimit / 1000000;
            this.ServiceCharge =
              (JSON.parse(res.data.data.feeTemplate).rate * 10000) / 100;

            this.sitesNo = res.data.data.list[0].accountNo;
            this.sites = res.data.data.list;
            this.radiovalue = this.sitesNo;
            this.bankCardNO = this.sitesNo;
            if (this.sites.length > 2) {
              this.secondBankList = this.sites.slice(0, 2);
              if (res.data.data.count >= 5) {
                this.showAddbank = false;
              }
              this.isshowAll = true;
            } else {
              this.secondBankList = this.sites;
              this.isshowAll = false;
            }
          } else {
            alert("交易系统未开放");
          }
        }
      });

      // this.$axios.post("/agent/recharge/open/fastPayment",{
      // }).then(res => {
      //   let dataJson = res.data.data;
      //   dataJson.code = '1234';
      //   console.log(res.data);
      //   this.$axios.post("/agent/recharge/open/fastPayment/verify",dataJson).then(res => {
      //     console.log(res)
      //   });
      // });
    },
    selectCBData() {
      this.spinning = true;
      this.$axios
        .get("/agent/recharge/select/UnitCustomer/bankCardInfo")
        .then(res => {
          console.log("res:", res);
          if (res.data.code == "OK") {
            this.spinning = false;
            // console.log("res:", res.data.data);
            // console.log("单日累计:", res.data.data.dayRechargeAmountSum / 100); //单位是分 要除100
            // console.log(
            //   "手续费:",
            //   JSON.parse(res.data.data.feeTemplate).rate * 100
            // );
            // console.log(
            //   "单笔限制:",
            //   res.data.data.product.singleAmountLimit / 100
            // );
            // console.log(
            //   "单日限制:",
            //   res.data.data.product.dayAmountLimit / 100
            // );
            // console.log("交易限制:", res.data.data.product.open);

            if ((res.data.data.product.open = true)) {
              this.dayRechargeAmountSum =
                res.data.data.dayRechargeAmountSum / 100;
              this.singleAmountLimit =
                res.data.data.product.singleAmountLimit / 1000000;
              this.dayAmountLimit =
                res.data.data.product.dayAmountLimit / 1000000;
              if (res.data.data.feeTemplate) {
                this.ServiceCharge =
                  JSON.parse(res.data.data.feeTemplate).rate * 100;
              }

              this.sitesNo = res.data.data.bankCardDTO.accountNo;
              this.sites = [];
              this.sites.push(res.data.data.bankCardDTO);
              // console.log(this.sites);
              // this.sites = res.data.data.bankCardDTO;
              console.log("site:", this.sites);
              this.radiovalue = this.sitesNo;
              this.bankCardNO = this.sitesNo;
              this.showAddbank = false;
              this.isshowAll = false;
              if (this.sites.length > 2) {
                this.secondBankList = this.sites.slice(0, 2);
                if (this.sites.length == 5) {
                  this.showAddbank = false;
                }
                this.isshowAll = true;
              } else {
                this.secondBankList = this.sites;
                this.isshowAll = false;
              }
            } else {
              alert("交易系统未开放");
            }
          }
        });
    },
    showAllbank() {
      this.showAllBankList = !this.showAllBankList;
      this.showAll = !this.showAllBankList ? "展开" : "折起";
    },
    liClick(e) {
      console.log(e);
    }
  }
};
</script>

<style scoped>
span {
  cursor: pointer;
}

.a_form_sty {
  padding-top: 10vh;
  margin-left: 50px;
  width: 70%;
}

.ant-btn-primary {
  background-color: #29a576;
  border-color: #29a576;
  padding-right: 20px;
  padding-left: 20px;
}

.cash {
  font-size: 26px;
  font-weight: bold;
  color: #279e71;
}

.group {
  display: flex;
  flex-direction: column;
}

.next {
  display: flex;
  flex-direction: row;
  width: 188px;
  margin: 0 auto;

  text-align: center;
}

.radio_card {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

ul {
  padding-left: 0;
}

.li_radio_style {
  list-style: none;
  border: 1px solid #ccc;
  border-radius: 3px;
  padding: 2px 10px;
  /* display: flex;
          align-items: center; */
  margin-bottom: 10px;
  width: 100%;
  overflow: hidden;
}

.bank_img {
  display: flex;
  align-items: center;
  float: left;
  height: 39px;
}

.li_radio_style img {
  width: 25px;
  height: 25px;
  vertical-align: middle;
  margin: 0 1%;
  float: left;
}

.li_radio_style .bank_info {
  float: left;
  margin-left: 2%;
}

.li_radio_style i {
  color: #153056;
  margin-right: 2%;
  float: right;
  font-stretch: normal;
}

.addBank {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-top: -15px;
}

#addBank {
  cursor: pointer;
  margin-top: 8px;
  border: 1px solid #bbb;
  border-radius: 5px;
  text-align: left;
  padding: 0px 10px;
  width: 100%;
}

.tooltip_sty {
  margin-left: 20px;
  color: #3b9fe2;
  padding: 0 15px;
  font-size: 12px;
  background-color: #f5fcff;
  border: 1px solid #3b9fe2;
  border-radius: 4px;
  position: relative;
  line-height: 30px;
}

.tooltip_sty:before {
  content: "";
  position: absolute;
  left: -17px;
  top: 50%;
  margin-top: -8px;
  border: 8px solid transparent;
  border-right-color: #3b9fe2;
}

.tooltip_sty:after {
  content: "";
  position: absolute;
  left: -15px;
  top: 50%;
  margin-top: -8px;
  border: 8px solid transparent;
  border-right-color: white;
}

.cash_out {
  display: flex;
  align-items: center;
}

.cash_out input {
  width: 40%;
}
</style>